﻿using System;
using System.Collections.Generic;
using System.Data;
using WebService.Nafania.DataLayer.Model.Base;

namespace WebService.Nafania.DataLayer.Model 
{
	internal partial class TemplateRepository
	{
		public Template AddTemplate(string key, string alias, string name, string type, string value, string comment = null, DateTime? date = null)
		{
			using (AdoRepository rep = Repository)
			{
				var spParams = new List<SpParam>()
				               	{
				               		new SpParam(){Name = "@Name", Type = DbType.String, Value = name },
				               		new SpParam(){Name = "@Type", Type = DbType.String, Value = type },
				               		new SpParam(){Name = "@Alias", Type = DbType.String, Value = alias },
				               		new SpParam(){Name = "@Value", Type = DbType.String, Value = value },
				               		new SpParam(){Name = "@Key", Type = DbType.String, Value = key },
				               	};

				if (comment != null) 
					spParams.Add(new SpParam() { Name = "@Comment", Type = DbType.String, Value = comment });
				if (date.HasValue) 
					spParams.Add(new SpParam() { Name = "@Date", Type = DbType.DateTime, Value = date.Value });

				return rep.ExecuteProcedureReader("[shell].[Template_AddTemplate]", spParams, Mapping);
			}
		}

		public Template GetByName(string @alias, string name, long? version)
		{
			using (AdoRepository rep = Repository)
			{
				var spParams = new List<SpParam>()
				               	{
				               		new SpParam(){Name = "@Name", Type = DbType.String, Value = name },
				               		new SpParam(){Name = "@Alias", Type = DbType.String, Value = alias },
				               	};

				if (version.HasValue)
					spParams.Add(new SpParam() { Name = "@Version", Type = DbType.String, Value = version.Value });

				return rep.ExecuteProcedureReader("[shell].[Template_GetByName]", spParams, Mapping);
			}
		}
	}
}